package cn.tedu.review;/**
 * @Author 金帮嘉诚 @Date 2021-06-24 星期四 19:05
 */
/*本类用于测试*/

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**知识点*/
public class TestReentrantReadWriteLock {
    public static void main(String[] args) {
        SaleTicketsV3 st1=new SaleTicketsV3();
        Thread t1=new Thread(st1);
        Thread t2=new Thread(st1);
        Thread t3=new Thread(st1);
        Thread t4=new Thread(st1);

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}
class SaleTicketsV3 implements Runnable{
    int ticketNumber =100;
    ReentrantReadWriteLock readWriteLock=new ReentrantReadWriteLock();
    @Override
    public void run() {
        while (true){
            readWriteLock.writeLock().lock();

            try {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticketNumber>0){
                    System.out.println(Thread.currentThread().getName()+"="+ticketNumber--);
                }
                if (ticketNumber<=0)break;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                readWriteLock.writeLock().unlock();
            }

        }
    }
}
